package tuding.android.bigplanettracks.util;


import tuding.android.bigplanettracks.maps.RawTile;
import tuding.android.bigplanettracks.maps.storage.SQLLocalStorage;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.util.Log;

/**
 * 
 */
public class Preferences {

	private static final String OFFSET_Y = "offsetY";

	private static final String OFFSET_X = "offsetX";

	private static final String TILEZ = "tilez";

	private static final String TILEY = "tiley";

	private static final String TILEX = "tilex";

	private static final String USE_NET = "useNet";

	private static final String SOURCE_ID = "sourceId";
	
	private static final String SQLite_Name = "SQLite";

	private static SharedPreferences prefs;

	public static final String MAP_SOURCE = "MAP_SOURCE";

	public static final String NETWORK_MODE = "NETWORK_MODE";

	public static final String MAP_MAGNIFICATION = "MAP_MAGNIFICATION";

	public static final String GPS_OFFSET_X = "GPS_OffsetX";
	public static final String GPS_OFFSET_Y = "GPS_OffsetY";
	
	public static final String USERNAME = "tudingUsername";
	public static final String PASSWORD = "tudingPassword";
	
	//public static int tudingUploadPrivacy = 0; //0:means all people can view
	//public static int gpsIntervalValue = 4000; // 4s per update
	//public static int gpsIntervalIndex = 0;
	
	public static final String LOGINED = "logined";
	
	public static final String VERSION_NUM = "tudingversion";
	
	public static int getTudingUploadPrivacy() {
		return prefs.getInt("TudingUploadPrivacy", 0);
	}
	
	public static void setTudingUploadPrivacy(int val) {
		put("TudingUploadPrivacy", val);
	}
	
	public static int getGPSIntervalValue() {
		return prefs.getInt("GPSIntervalValue", 4000);
	}
	
	public static void setGPSIntervalValue(int val) {
		put("GPSIntervalValue", val);
	}
	
	public static int getGPSIntervalIndex() {
		return prefs.getInt("GPSIntervalIndex", 0);
	}
	
	public static void setGPSIntervalIndex(int index) {
		put("GPSIntervalIndex", index);
	}
	
	public static void setAutoRecordIntervalIndex(int val) {
		put("AutoRecordIntervalIndex", val);
	}
	
	public static int getAutoRecordIntervalIndex() {
		return prefs.getInt("AutoRecordIntervalIndex", 0);
	}
	
	public static int getAutoRecordIntervalVal() {
		int tmp = getAutoRecordIntervalIndex();
		if(tmp == 0)      return 300000;
		else if(tmp == 1) return 600000;
		else if(tmp == 2) return 900000;
		else              return 1800000;
	}


	public static boolean isLogined() {
		return prefs.getBoolean(Preferences.LOGINED, false);
	}
	
	public static void setLogined(boolean logined) {
		put(Preferences.LOGINED, logined);
	}
	
	public static void init(Application app) {
		prefs = app.getSharedPreferences("global", Context.MODE_PRIVATE);
	}
	
	public static void putUsername(String username) {
		put(Preferences.USERNAME, username);
	}
	
	public static String getUsername() {
		return prefs.getString(Preferences.USERNAME, "");
	}
	
	public static void putPassword(String password) {
		put(Preferences.PASSWORD, password);
	}
	
	public static String getPassword() {
		return prefs.getString(Preferences.PASSWORD, "");
	}

	/**
	 * Сохраняет угловой тайл
	 * 
	 * @param tile
	 */
	public static void putTile(RawTile tile) {
		put(Preferences.TILEX, tile.x);
		put(Preferences.TILEY, tile.y);
		put(Preferences.TILEZ, tile.z);
	}

	/**
	 * Возвращает угловой тайл
	 * 
	 * @return
	 */
	public static RawTile getTile() {
		int x, y, z; // Taiwan = (106, 54, 13)
		x = prefs.getInt(Preferences.TILEX, 25); //0
		y = prefs.getInt(Preferences.TILEY, 10);  //0
		z = prefs.getInt(Preferences.TILEZ, 8);  //16
		return new RawTile(x, y, z, -1);
	}

	/**
	 * Сохраняет отступ
	 * 
	 * @param offset
	 */
	public static void putOffset(Point offset) {
		put(Preferences.OFFSET_X, offset.x);
		put(Preferences.OFFSET_Y, offset.y); 
		Log.i("PREF", "offset is :"+offset.x+" "+offset.y);
	}

	/**
	 * Возвращает отступ
	 * 
	 * @return
	 */
	public static Point getOffset() {
		// Taiwan = (-85, -156)
		int x = prefs.getInt(Preferences.OFFSET_X, -85);  //0
		int y = prefs.getInt(Preferences.OFFSET_Y, -156); //0
		return new Point(x, y);
	}

	public static void putGPSOffset(Point offset) {
		put(Preferences.GPS_OFFSET_X, offset.x);
		put(Preferences.GPS_OFFSET_Y, offset.y);
	}

	public static Point getGPSOffset() {
		// Taiwan = (0, 0), only China need to set these values
		int x = prefs.getInt(Preferences.GPS_OFFSET_X, 0);
		int y = prefs.getInt(Preferences.GPS_OFFSET_Y, 0);
		return new Point(x, y);
	}

	public static void putSourceId(int sourceId) {
		if(sourceId==-1){
			throw new IllegalArgumentException();
		}
		put(SOURCE_ID, sourceId);
	}

	public static int getSourceId() {
		int sourceId = prefs.getInt(Preferences.SOURCE_ID, 0);
		return sourceId;
	}

	public static void putSQLiteName(String strSQLiteName) {
		put(Preferences.SQLite_Name, strSQLiteName);
	}

	public static String getSQLiteName() {
		String strSQLiteName = prefs.getString(Preferences.SQLite_Name, SQLLocalStorage.SQLITEDB);
		return strSQLiteName;
	}

	public static void putUseNet(boolean useNet) {
		put(Preferences.USE_NET, useNet);
	}

	public static boolean getUseNet() {
		boolean useNet = prefs.getBoolean(Preferences.USE_NET, true);
		return useNet;
	}

	public static void putMapMagnificationIndex(int mapMagnificationIndex) {
		put(Preferences.MAP_MAGNIFICATION, mapMagnificationIndex);
	}

	public static int getMapMagnificationIndex() {
		int mapMagnificationIndex = prefs.getInt(Preferences.MAP_MAGNIFICATION, 0);
		return mapMagnificationIndex;
	}

	public static void put(String name, Object value){
		SharedPreferences.Editor editor = prefs.edit();
		if(value.getClass() == Boolean.class){
			editor.putBoolean(name, (Boolean)value);
		}
		if(value.getClass() == String.class){
			editor.putString(name, (String)value);
		}
		if(value.getClass() == Integer.class){
			editor.putInt(name, ((Integer)value).intValue());
		}
		
		
		editor.commit();
		
	}
	
	
    public static boolean getBoolean(String key, boolean defaultValue) {
        return prefs.getBoolean(key, defaultValue);
    }
    
    public static int getInt(String key) {
        return prefs.getInt(key, 0);
    }
    
    public static int getInt(String key, int defaultValue) {
        return prefs.getInt(key, defaultValue);
    }
    
    public static float getFloat(String key) {
        return prefs.getFloat(key, 0);
    }
    public static long getLong(String key) {
        return prefs.getLong(key, 0);
    }
    
    public static String getString(String key) {
        return prefs.getString(key, "");
    }
    
    public static String getString(String key, String defaultValue) {
        return prefs.getString(key, defaultValue);
    }
    
    public  static boolean containsKey(String key){
        return(prefs.contains(key));
    }
    
    /**
     * Delete the record associated with this key
     * @param key key of data need to be deleted
     */
    public static void delete(String key) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove(key);
        editor.commit();
    }
	/**
	 * Store the detected newest version of the app retrieved from detecting server
	 * @param version the newest version detected 
	 */
	public static void putVersionNum(String version) {
        put(Preferences.VERSION_NUM, version);
    }
    
	/**
	 * get the detected newest version of the app retrieved from detecting server
	 * @return the detected newest version of the app retrieved from detecting server
	 */
    public static String getVersionNum(){
        return prefs.getString(Preferences.VERSION_NUM, "");
    }

    
	
	
}
